package com.qk.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> list(UserDto userDto) {
        //创建PageHelper对象,设置起始页和每页记录数
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        //调用mapper接口,执行分页查询
        List<User> userList = userMapper.selectList(userDto);

        //封装PageResult对象并返回
        Page<User> p = (Page<User>) userList;

        return new PageResult<>(p.getTotal(), p.getResult());
    }
    //添加用户
    @Override
    public void add(User user) {
        //补全信息
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        //密码加密
        String password = user.getUsername() + "123";
        // 设置初始密码
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        //调用mapper接口
        userMapper.add(user);
    }
    //根据id查询用户
    @Override
    public User getById(Integer id) {
        return userMapper.selectByid(id);
    }

    @Override
    public void update(User user) {
        //补全信息
        user.setUpdateTime(LocalDateTime.now());
        //用户名变更，密码需要重新加密
        if(!user.getUsername().equals(getById(user.getId()).getUsername())){
            String password = user.getUsername() + "123";
            user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        }
        //调用mapper接口
        userMapper.update(user);
    }

    //批量删除用户
    @Override
    public void deleteByIds(List<Integer> ids) {
        userMapper.deleteByIds(ids);
    }

    //根据角色标识查询用户
    @Override
    public List<User> getByRoleLabel(String roleLabel) {
        return userMapper.selectByRoleLabel(roleLabel);
    }

    //根据部门id查询用户
    @Override
    public List<User> getByDeptId(Integer deptId) {
        return userMapper.selectByDeptId(deptId);
    }
    //用户登录
    @Override
    public LoginResultVo login(User user) {
       User u = userMapper.selectByUserName(user.getUsername());

       //判断用户是否存在
       if (u == null || !u.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))){
           log.info("用户名或密码错误");
           throw new BusinessException("用户名或密码错误");
       }

       //判断用户状态状态是否正确
        if (u.getStatus() != 1){
            log.info("用户已停用");
            throw new BusinessException("用户已停用");
        }
        //封装返回结果
        LoginResultVo loginResult = new LoginResultVo();
        loginResult.setId(u.getId());
        loginResult.setUsername(u.getUsername());
        loginResult.setName(u.getName());
        loginResult.setImage(u.getImage());
        loginResult.setRoleLabel(u.getRoleLabel());

        //调用工具类,生成Jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", u.getId());
        claims.put("username", u.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        loginResult.setToken(jwt);

        return loginResult;


    }

}
